package pt.iscte.pcd.accounts;

public class Main { // { extends Thread {

	private static final int INITIAL_BALANCE = 1000;

	public static void main(String[] args) {
		Main m = new Main();
		m.execute();
	}

	private void execute() {
		// Create an account with an initial balance of 1000
		Account c = new Account(INITIAL_BALANCE);
		// Create a depositor and a withdrawer
		Depositer d = new Depositer(c);
		Withdrawer w = new Withdrawer(c);
		// Start the depositor and the withdrawer threads
		d.start();
		w.start();
		// Sleep for 10 seconds
		try {
			Thread.sleep(10000);
		} catch (InterruptedException e) {

		}
		// Interrupt the depositor and the withdrawer
		d.interrupt();
		w.interrupt();
		// Make sure that both the depositor and the withdrawer threads have
		// stopped.
		try {
			d.join();
			w.join();
			System.out.println("Joined");
		} catch (InterruptedException e) {
		}

		// Print out the initial balance, the total amount deposited, the total
		// amount withdrawn.
		System.out.println("Initial balance " + INITIAL_BALANCE);
		System.out.println("Total deposited " + d.totalAmountDeposited());
		System.out.println("Total widthrawn " + w.totalAmountWithdrawn());
		System.out.println("Current balance " + c.getBalance());
		// Check that the balance of the account is consistent. That is, check
		// that
		// "the initial balance + total amount deposited - total amount withdrawn"
		// is equal to what the account says that its balance is.
		int check = INITIAL_BALANCE + d.totalAmountDeposited()
				- w.totalAmountWithdrawn();
		System.out.println("Balance check " + check);
	}

}
